package org.jeecg.modules.yw.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yw.entity.EnterPrise;
import org.jeecg.modules.yw.entity.EnterpriseStatic;
import org.jeecg.modules.yw.service.WorkerStaticService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.jeecg.modules.yw.vo.WorkerStatic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "订单统计")
@RequestMapping("/workerStatic")
public class WorkerStaticController extends JeecgController<WorkerStatic, WorkerStaticService> {
    @Autowired
    private WorkerStaticService workerStaticService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @ApiOperation(value = "工人统计")
    @GetMapping("/page")
    public  Result page(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "position", required = false) String position,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize ) {
        Page<WorkerStatic> page = new Page<>(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
        String[] areas=null;
        if (!roleIds.contains(CommonUtils.ADMIN)) {
            String area=sysUser.getArea();
            if(org.apache.commons.lang3.StringUtils.isNotBlank(area)){
                areas=area.split(",");
            }else{
                IPage<EnterPrise> result =new Page<>();
                return org.jeecg.common.api.vo.Result.ok(result);
            }
        }
        return Result.OK(workerStaticService.pageInfo(page,name,position,areas));
    }
    /**
     * 导出excel
     *
     * @param request
     * @param name
     * @param position
     */
    //@RequiresPermissions("org.jeecg.modules:rewardspunish:exportXls")
    @GetMapping(value = "/exportXls")
    @ApiOperation(value = "导出工人统计")
    public ModelAndView exportXls(HttpServletRequest request,
                                  @RequestParam(value = "ids", required = false) String ids,
                                  @RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "position", required = false) String position) {
        String[] idss=null;
        if(StringUtils.isNotBlank(ids)){
            idss=ids.split(",");
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
        String user= UserUtil.getUserName(request);
        String[] areas=null;
        if (!roleIds.contains(CommonUtils.ADMIN)) {
            String area=sysUser.getArea();
            if(org.apache.commons.lang3.StringUtils.isNotBlank(area)){
                areas=area.split(",");
            }else{
                return super.exportXls(new ArrayList<>(), WorkerStatic.class,user, "工人统计");
            }
        }
        List<WorkerStatic> list=workerStaticService.listInfo(idss,name,position,areas);
        return super.exportXls(list, WorkerStatic.class,user, "工人统计");
    }
}
